<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="168px">
      <el-form-item label="BOM编号" prop="code">
        <el-input
          v-model="queryParams.code"
          placeholder="请输入BOM编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
<!--      <el-form-item label="成品" prop="productId">-->
<!--        <el-select v-model="queryParams.productId" placeholder="全部" clearable :style="{width: '100%'}">-->
<!--          <el-option v-for="(item, index) in productList" :key="index" :label="item.productName"-->
<!--                     :value="item.id" ></el-option>-->
<!--        </el-select>-->
<!--      </el-form-item>-->
      <el-form-item label="是否有效" prop="isActive">
        <el-select v-model="queryParams.isActive" placeholder="全部" clearable :style="{width: '100%'}">
          <el-option label="有效" value="1" ></el-option>
          <el-option label="无效" value="0" ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:Bom:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:Bom:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="BomList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" show-overflow-tooltip/>
      <el-table-column label="BOM编号" align="center" prop="code" width="190"/>
      <el-table-column label="成品名称" align="center" prop="product.productName" />
      <el-table-column label="是否有效" align="center" prop="isActive" >
        <template slot-scope="scope">
          <span>{{ scope.row.isActive == 1 ? "有效" : "无效" }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作人" align="center" prop="createBy" />
      <el-table-column label="操作时间" align="center" prop="createTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center" prop="remark" show-overflow-tooltip/>
      <el-table-column label="操作" align="left" class-name="small-padding fixed-width" width="220" fixed="right">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-view"
            @click="selectOrder(scope.row)"
          >查看</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:Bom:edit']"
          >修改</el-button>
<!--          <el-button-->
<!--            size="mini"-->
<!--            type="text"-->
<!--            icon="el-icon-delete"-->
<!--            @click="handleDelete(scope.row)"-->
<!--            v-hasPermi="['system:Bom:remove']"-->
<!--          >删除</el-button>-->
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 查看按钮 -->
    <el-dialog :title="title" :visible.sync="open" width="1000px" append-to-body>
      <div style="border: 1px gainsboro solid;padding: 10px 10px 10px 30px">
        <div v-if="open === true" style="display: inline-block;width: 300px;"><p>商品：{{ this.form.product.productName }}</p></div>
        <div v-if="open === true" style="display: inline-block;width: 300px;"><p>商品类型：{{ this.form.product.categoryName }}</p></div>
        <div v-if="open === true" style="display: inline-block;width: 300px;"><p>规格型号：{{ this.form.product.specification }}</p></div>
        <br>
        <div v-if="open === true" style="display: inline-block;width: 300px;"><p>生产周期(天)：{{ this.form.product.productionCycle }}</p></div>
<!--        <div v-if="open === true" style="display: inline-block;width: 300px;"><p>质量标准：{{ this.form.product.qualityStandard }}</p></div>-->
        <div v-if="open === true" style="display: inline-block;width: 300px;"><p>状态：<span>{{ this.form.product.status==0 ? '下架' : (this.form.product.status==1 ? '上架' : '停产') }}</span></p></div>
        <br>
        <div style="display: inline-block;width: 300px;"><p>操作人：{{ this.form.createBy }}</p></div>
        <div style="display: inline-block;width: 300px;"><p>操作时间：{{ parseTime(this.form.createTime, '{y}-{m}-{d}') }}</p></div>
      </div>
      <div style="border: 1px gainsboro solid;margin-top: 5px">
        <el-table v-loading="loading" :data="this.bomItemList" >
          <el-table-column label="物料编号" align="left" prop="material.materialCode"  show-overflow-tooltip width="200px" />
          <el-table-column label="物料名称" align="left" prop="material.materialName" />
          <el-table-column label="物料类型" align="left" prop="material.materialType" >
            <template slot-scope="scope">
              <span>{{ scope.row.material.materialType == 1 ? "原材料" : scope.row.material.materialType == 2 ? "半成品" : scope.row.material.materialType == 3 ? "成品": "辅料" }}</span>
            </template>
          </el-table-column>
          <el-table-column label="规格型号" align="left" prop="material.specification" />
          <el-table-column label="类型" align="left" prop="material.categoryName" />
          <el-table-column label="标准单价" align="left" prop="material.standardPrice" />
          <el-table-column label="备注" align="left" prop="remark" width="100px"/>
        </el-table>
      </div>
      <div style="border: 1px gainsboro solid;margin-top: 5px;padding: 10px;text-align: right">
        <label >备注： </label>
        <textarea style="width: 90%;height: 50px;margin-top: 10px;margin-left: 10px;margin-bottom: 10px;border-radius: 5px;vertical-align:middle;border: 1px gainsboro solid" v-model="this.form.remark" disabled/>
      </div>

      <div slot="footer" class="dialog-footer" style="text-align: center">
        <el-button type="primary" @click="handlePrint()">打 印</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>
import { listBom, getBom, delBom, addBom, updateBom } from "@/api/system/Bom";
import { listProduct } from "@/api/system/Product";
import {parseTime} from "../../../utils/ruoyi";

export default {
  name: "Bom",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 子表选中数据
      checkedBomItem: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 物料清单表格数据
      BomList: [],
      // BOM明细表格数据
      bomItemList: [],
      // 成品列表数据
      productList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        bomNo: null,
        productId: null,
        isActive: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        bomNo: [
          { required: true, message: "BOM编号不能为空", trigger: "blur" }
        ],
        productId: [
          { required: true, message: "商品ID不能为空", trigger: "blur" }
        ],
        version: [
          { required: true, message: "版本号不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    parseTime,
    beforeRouteEnter(to, from, next) {
      next(vm => {
        vm.getList(); // 进入页面时刷新数据
      });
    },
    /** 查询物料清单列表 */
    getList() {
      this.loading = true;
      listBom(this.queryParams).then(response => {
        this.BomList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
      listProduct().then(response => {
        this.productList = response.rows;
      })
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        bomNo: null,
        productId: null,
        version: null,
        isActive: null,
        remark: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null
      };
      this.bomItemList = [];
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    // 查看按钮
    selectOrder(row){
      const id = row.id || this.ids
      getBom(id).then(response => {
        this.form = response.data;
        this.bomItemList = response.data.bomItemList;
        console.log(this.form)
        this.open = true;
        this.title = "查看";
      });
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.$router.push({
        path: '/Bom/add',
      })
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      this.$router.push({
        path: '/Bom/update',
        query:{
          id: id,
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除物料清单编号为"' + ids + '"的数据项？').then(function() {
        return delBom(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
	/** BOM明细序号 */
    rowBomItemIndex({ row, rowIndex }) {
      row.index = rowIndex + 1;
    },
    /** BOM明细添加按钮操作 */
    handleAddBomItem() {
      let obj = {};
      obj.materialId = "";
      obj.quantity = "";
      obj.unit = "";
      obj.lossRate = "";
      obj.remark = "";
      obj.sort = "";
      this.bomItemList.push(obj);
    },
    /** BOM明细删除按钮操作 */
    handleDeleteBomItem() {
      if (this.checkedBomItem.length == 0) {
        this.$modal.msgError("请先选择要删除的BOM明细数据");
      } else {
        const bomItemList = this.bomItemList;
        const checkedBomItem = this.checkedBomItem;
        this.bomItemList = bomItemList.filter(function(item) {
          return checkedBomItem.indexOf(item.index) == -1
        });
      }
    },
    /** 复选框选中数据 */
    handleBomItemSelectionChange(selection) {
      this.checkedBomItem = selection.map(item => item.index)
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/Bom/export', {
        ...this.queryParams
      }, `Bom_${new Date().getTime()}.xlsx`)
    }
  },
  beforeRouteEnter(to, from, next) {
    next(vm => {
      // 重新获取数据
      vm.getList();
    });
  }
};
</script>
